<?php
namespace Model;

use Think\Model;
use \Home\Common\Aes;


class UserModel extends Model
{
    //设置验证规则
    //是否批处理验证
	// protected $patchValidate = true;
	//自动验证定义
	

	protected $_validate = array(
		//①验证手机号码是否为纯数字
		array('user_account', 'number', '{"msg":"手机号格式错误"}'),
		
		//②验证手机号码是否为11位
		array('user_account', '11', '{"msg":"请输入正确的手机号码"}', 0, 'length'),
		
		//③验证手机号码是否已被注册
		array('user_account', '', '{"msg":"该手机已被注册"}', 0, 'unique'),
		

		//验证微信号
		array('user_account', '', '{"msg":"该手机已被注册"}', 0, 'unique'),

	);
    
    //检验密码
	public function check_password($pw1, $pw2)
	{
		if ($pw1 != $pw2) {
			echo "两次输入的密码不一致!";
			return false;
		}
		return true;
	}
    
	//用户身份认证
	public function user_realname_auth($user_id, $realName, $user_card_no, $user_sex)
	{
		$User = M("user"); // 实例化User对象
		// 更改用户的name和email的值
		$data = array(
			'user_card_no' => $user_card_no,
			'user_sex' => $user_sex,
			'user_realname' => $realName,
			'user_realname_identification' => true
		);
		$User->where('user_id=' . $user_id)->setField($data);

	}


	//修改用户昵称
	public function model_update_user_nickname($user_id, $user_new_nickname)
	{
		$User = M("user"); // 实例化User对象
		// 更改用户的name和email的值
		$data = array(
			'user_nickname' => $user_new_nickname
		);
		$info = $User->where('user_id=' . $user_id)->setField($data);
		return $info;
	}
	

	//修改用户微信账号
	public function model_update_user_wechat($user_id, $user_wechat)
	{
		$User = M("user"); // 实例化User对象
		// 更改用户的name和email的值
		$data = array(
			'user_wechat' => $user_wechat
		);
		$info = $User->where('user_id=' . $user_id)->setField($data);
		return $info;
	}


	//修改用户默认联系方式
	public function model_update_user_default_contact_way($user_id, $user_default_contact_way) {
		$User = M("user"); // 实例化User对象
		// 更改用户的name和email的值
		$data = array(
			'user_default_contact_way' => $user_default_contact_way
		);
		$info = $User->where('user_id=' . $user_id)->setField($data);
		return $info;
	}


	//修改用户联系方式
	public function model_update_user_contact_way($user_id, $user_tel, $user_wechat, $user_qq, $user_default_contact_way)
	{
		$User = M("user"); // 实例化User对象
		$data = array(
			'user_wechat' => $user_wechat,
			'user_tel' => $user_tel,
			'user_qq' => $user_qq,
			'user_default_contact_way' => $user_default_contact_way
		);
		$info = $User->where('user_id=' . $user_id)->setField($data);
		return $info;

	}

	//获取用户信息
	public function model_get_user_information($user_id)
	{
		$user = D('user');
		$sql = "SELECT 
		user_realname,
		user_nickname,
		user_head_img,
		user_head_small_img,
		user_sex,
		user_school,
		user_student_identification,
		user_default_contact_way,
		user_realname_identification,
		user_tel,
		user_wechat,
		user_qq,
		user_birthday,
		user_grade,
		user_share_number,
		user_share_time,
		user_comment_number,
		user_score

		FROM
		wx_user
		WHERE
		user_id=" . $user_id . ";";

		$info = $user->query($sql);

		return $info;

	}


	//修改用户密码
	public function model_update_user_password($user_id, $user_password)
	{
		$User = M("user"); // 实例化User对象
		$data = array(
			'user_password' => $user_password
		);
		$info = $User->where('user_id=' . $user_id)->setField($data);
		return $info;

	}

	//修改学生认证状态
	public function model_update_user_student_state($user_id, $ident_state)
	{
		$User = M("user"); // 实例化User对象
		$data = array(
			'user_student_identification' => $ident_state,
		);
		$info = $User->where('user_id=' . $user_id)->setField($data);
		return $info;
	}


	//增加钱包金额
	public function wallet_increase($user_id, $number)
	{
		//实例aes加密类
		$aes = new AES;
		// $info = $aes->AESEncryptRequest("0.00");
		// echo $info;exit;
		//获取用户的金额
		$user = D('user');
		$crypt_user_money = $user->where("user_id=".$user_id)->getField('user_money');

		//对用户金额解密
		$money = $aes ->AESDecryptResponse($crypt_user_money);
		// echo $money;exit;
		// 修改新余额
		$new_money = $money + $number; 

		//新余额加密
		$crypt_new_money = $aes->AESEncryptRequest($new_money);
		
		//新余额入库
		$data = array(
			'user_money' => $crypt_new_money,
		);
		$info = $user->where('user_id=' . $user_id)->setField($data);
		return $info;		//返回数据库是否操作成功
		
	}




}